package com.gopay.bis.service.bank.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.bis.common.util.SendSmsUtils;
import com.gopay.bis.service.bank.BankIntrChannConfService;
import com.gopay.bis.service.bank.payconfig.PayConfigCommonService;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bis.BaseBisBankConfig;
import com.gopay.common.domain.bis.BaseBisBankExtConfig;
import com.gopay.common.domain.bis.BisBankBizIntrConfig;
import com.gopay.common.domain.bis.BisBcdlIntrConfig;
import com.gopay.common.exception.GopayException;

/**
 * 银行接口通道配置信息
 * @author liujy
 *
 */
@Service("bankIntrChannConfService")
public class BankIntrChannConfServiceImpl implements BankIntrChannConfService{
	

	@Resource(name = "smsConfig")
	private Map<String, String> smsConfig;
	
	@Resource(name="payConfigCommonService")
	private PayConfigCommonService payConfigCommonService;
	
	/**
     * 根据银行业务产品代码及银行码查询接口配置
     * @param bankBusProd
     * @param bankCode
     * @return
     */
    public BaseBisBankExtConfig findBaseBisBankExtConfig(BisBankBusinessProd bankBusProd, String bankCode){
    	return (BaseBisBankExtConfig)payConfigCommonService.getBankInreChanConfigsByBankCodeAndProd(bankCode, bankBusProd);
    }
    
    /**
     * 银企直连接口配置
     * @param bankCode
     * @return
     */
    public BisBcdlIntrConfig findBcdlIntrConfig(String bankCode){
    	return (BisBcdlIntrConfig)payConfigCommonService.getBankInreChanConfigsByBankCodeAndProd(bankCode, BisBankBusinessProd._09);
    }
    
    /**
	 * 校验银行业务产品是否支持该通道
	 * @param chCode
	 * @param bankBusProd
	 * @throws GopayException 
	 */
    public void checkBankBussProdAndIntrChan(String chCode,
			String bankBusProd) throws GopayException{
    	BisBankBizIntrConfig config = payConfigCommonService.getBisBankBizIntrConfig(chCode, bankBusProd);
    	if(null == config){
    		SendSmsUtils.sendSmsErr( smsConfig.get("switch"), smsConfig.get("mobile"),ProcCodeConstants.PROC_CODE_100E7022);
    		throw new GopayException(ProcCodeConstants.PROC_CODE_100E7022);
    	}
    }

	@Override
	public void checkBankIntrChan(BaseBisBankConfig config, OutStlmTranCode outTranCode)
			throws GopayException {
		if(OutStlmTranCode._1000.equals(outTranCode)){
			boolean ispay;// 支付
			ispay = ((BaseBisBankExtConfig)config).isPay();
			if(false == ispay){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7015);
			}
			return;
		}
		// 单笔转账
		if(OutStlmTranCode._1001.equals(outTranCode)){
			boolean issignTransfer;// 单笔转账
			issignTransfer = ((BisBcdlIntrConfig)config).isSingleTransfer();
			if(false == issignTransfer){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7018);
			}
			return;
		}
		if(OutStlmTranCode._1501.equals(outTranCode)){
			boolean isbatchTransfer;// 批量转账
			isbatchTransfer = ((BisBcdlIntrConfig)config).isBatchTransfer();
			if(false == isbatchTransfer){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7019);
			}
			return;
		}
		if(OutStlmTranCode._2000.equals(outTranCode)){
			boolean isrefund;// 退款
			isrefund = ((BaseBisBankExtConfig)config).isRefund();
			if(false == isrefund){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7020);
			}
			return;
		}
		if(OutStlmTranCode._3000.equals(outTranCode)){
			boolean ischeckorder;// 查询
			if(config instanceof BisBcdlIntrConfig){
				ischeckorder = ((BisBcdlIntrConfig)config).isCheckOrder();
			}else if(config instanceof BaseBisBankExtConfig){
				ischeckorder = ((BaseBisBankExtConfig)config).isCheckOrder();
			}else{
				return;
			}
			if(false == ischeckorder){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7016);
			}
			return;
		}
		if(OutStlmTranCode._3001.equals(outTranCode)){
			boolean isreconciliation;// 对账
			if(config instanceof BisBcdlIntrConfig){
				isreconciliation = ((BisBcdlIntrConfig)config).isReconciliation();
			}else if(config instanceof BaseBisBankExtConfig){
				isreconciliation = ((BaseBisBankExtConfig)config).isReconciliation();
			}else{
				return;
			}
			if(false == isreconciliation){
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7017);
			}
			return;
		}
	}

	/**
     * 根据银行业务产品代码、渠道及银行码查询接口配置
     * 
     * @param prod
     * @param channel
     * @param bankCode
     * @return
     */
    public BaseBisBankExtConfig findBaseBisBankExtConfigByBankChannel(BisBankBusinessProd prod,
            BisBankInterfaceChannel channel, String bankCode) {
        return (BaseBisBankExtConfig)payConfigCommonService.getBankInreChanConfigsByBankChannelAndProd(bankCode, prod, channel);
    }
}













